﻿using SafeLoad.NControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace FPSO.HMI.Pipe
{
    class FourPointElement : IPipeElement
    {
        public FourPointElement(NPipeLineFour fourPoint) : base()
        {
            FourPoint = fourPoint;
            Port1 = null;
            Port2 = null;
            Port3 = null;
            Port4 = null;

            double halfWidth = fourPoint.ActualWidth / 2;
            double halfHeight = fourPoint.ActualHeight / 2;

            //四通管不会旋转，不用转换
            Pos = new Point()
            {
                X = Canvas.GetLeft(fourPoint) + fourPoint.ActualWidth / 2,
                Y = Canvas.GetTop(fourPoint) + fourPoint.ActualHeight / 2
            };

            Port1Pos = new Point(Pos.X, Pos.Y - halfHeight);
            Port2Pos = new Point(Pos.X + halfWidth, Pos.Y);
            Port3Pos = new Point(Pos.X, Pos.Y + halfHeight);
            Port4Pos = new Point(Pos.X - halfWidth, Pos.Y);
        }

        public IPipeElement Port1 { get; set; }
        public IPipeElement Port2 { get; set; }
        public IPipeElement Port3 { get; set; }
        public IPipeElement Port4 { get; set; }

        public NPipeLineFour FourPoint { get; private set; }

        public Point Pos { get; private set; }

        public Point Port1Pos { get; private set; }
        public Point Port2Pos { get; private set; }
        public Point Port3Pos { get; private set; }
        public Point Port4Pos { get; private set; }

        public void Connect(PipeLineElement pipeLine)
        {
            bool flag = false;
            double dis1 = 0;
            double dis2 = 0;
            double dis3 = 0;
            double dis4 = 0;

            if (PointDistance.Distance(Pos, pipeLine.Port1Pos) < 20 && pipeLine.Port1 == null)
            {
                flag = true;
                pipeLine.Port1 = this;
                dis1 = PointDistance.Distance(pipeLine.Port1Pos, Port1Pos);
                dis2 = PointDistance.Distance(pipeLine.Port1Pos, Port2Pos);
                dis3 = PointDistance.Distance(pipeLine.Port1Pos, Port3Pos);
                dis4 = PointDistance.Distance(pipeLine.Port1Pos, Port4Pos);
            }
            else if (PointDistance.Distance(Pos, pipeLine.Port2Pos) < 20 && pipeLine.Port2 == null)
            {
                flag = true;
                pipeLine.Port2 = this;
                dis1 = PointDistance.Distance(pipeLine.Port2Pos, Port1Pos);
                dis2 = PointDistance.Distance(pipeLine.Port2Pos, Port2Pos);
                dis3 = PointDistance.Distance(pipeLine.Port2Pos, Port3Pos);
                dis4 = PointDistance.Distance(pipeLine.Port2Pos, Port4Pos);
            }

            if (flag)
            {
                if (dis1 < dis2 && dis1 < dis3 && dis1 < dis4)
                {
                    Port1 = pipeLine;
                }
                if (dis2 < dis1 && dis2 < dis3 && dis2 < dis4)
                {
                    Port2 = pipeLine;
                }
                if (dis3 < dis1 && dis3 < dis2 && dis3 < dis4)
                {
                    Port3 = pipeLine;
                }
                if (dis4 < dis1 && dis4 < dis2 && dis4 < dis3)
                {
                    Port4 = pipeLine;
                }
            }
        }

        public PortState Port1State { get; private set; }
        public PortState Port2State { get; private set; }
        public PortState Port3State { get; private set; }
        public PortState Port4State { get; private set; }

        public override void ResetState()
        {
            Port1State = PortState.Stop;
            Port2State = PortState.Stop;
            Port3State = PortState.Stop;
            Port4State = PortState.Stop;
        }

        public override void SetInPort(IPipeElement element)
        {
            if (Port1 == element)
            {
                Port1State = PortState.In;
            }
            else if (Port2 == element)
            {
                Port2State = PortState.In;
            }
            else if (Port3 == element)
            {
                Port3State = PortState.In;
            }
            else if (Port4 == element)
            {
                Port4State = PortState.In;
            }
        }

        public override void SetOutPort(IPipeElement element)
        {
            if (Port1 == element)
            {
                Port1State = PortState.Out;
            }
            else if (Port2 == element)
            {
                Port2State = PortState.Out;
            }
            else if (Port3 == element)
            {
                Port3State = PortState.Out;
            }
            else if (Port4 == element)
            {
                Port4State = PortState.Out;
            }
        }

        public override void UpdateUI()
        {
            bool active;
            double speed;

            active = Port1State != PortState.Stop;
            speed = Port1State == PortState.In ? -0.35d : 0.35d;
            if (FourPoint.PipeLineActive1 != active)
            {
                FourPoint.PipeLineActive1 = active;
            }
            if (Math.Abs(speed - FourPoint.MoveSpeed1) > 0.1)
            {
                FourPoint.MoveSpeed1 = speed;
            }

            active = Port2State != PortState.Stop;
            speed = Port2State == PortState.In ? -0.35d : 0.35d;
            if (FourPoint.PipeLineActive2 != active)
            {
                FourPoint.PipeLineActive2 = active;
            }
            if (Math.Abs(speed - FourPoint.MoveSpeed2) > 0.1)
            {
                FourPoint.MoveSpeed2 = speed;
            }

            active = Port3State != PortState.Stop;
            speed = Port3State == PortState.In ? -0.35d : 0.35d;
            if (FourPoint.PipeLineActive3 != active)
            {
                FourPoint.PipeLineActive3 = active;
            }
            if (Math.Abs(speed - FourPoint.MoveSpeed3) > 0.1)
            {
                FourPoint.MoveSpeed3 = speed;
            }

            active = Port4State != PortState.Stop;
            speed = Port4State == PortState.In ? -0.35d : 0.35d;
            if (FourPoint.PipeLineActive4 != active)
            {
                FourPoint.PipeLineActive4 = active;
            }
            if (Math.Abs(speed - FourPoint.MoveSpeed4) > 0.1)
            {
                FourPoint.MoveSpeed4 = speed;
            }


            //FourPoint.PipeLineActive1 = Port1State != PortState.Stop;
            //if (Port1State != PortState.Stop)
            //{
            //    FourPoint.MoveSpeed1 = Port1State == PortState.In ? -0.35d : 0.35d;
            //}

            //FourPoint.PipeLineActive2 = Port2State != PortState.Stop;
            //if (Port2State != PortState.Stop)
            //{
            //    FourPoint.MoveSpeed2 = Port2State == PortState.In ? -0.35d : 0.35d;
            //}

            //FourPoint.PipeLineActive3 = Port3State != PortState.Stop;
            //if (Port3State != PortState.Stop)
            //{
            //    FourPoint.MoveSpeed3 = Port3State == PortState.In ? -0.35d : 0.35d;
            //}

            //FourPoint.PipeLineActive4 = Port4State != PortState.Stop;
            //if (Port4State != PortState.Stop)
            //{
            //    FourPoint.MoveSpeed4 = Port4State == PortState.In ? -0.35d : 0.35d;
            //}


        }
    }
}
